<template>
    <div class="home" style=" padding: 10px">
        <!--  功能区域  -->
        <div class="flex-bar">
            <!--  搜索区域  -->
            <div style="margin: 10px 0">
                <el-input v-model="search" placeholder="请输入要查询的房屋地址" style="width: 300px; margin-right: 5px" clearable @keyup.enter="load"></el-input>
                <el-button type="primary" style="margin-right:20px; margin-left: 5px" @click="load" >查询</el-button>
            </div>
            <div style="margin: 10px 0">
                <el-button type="primary" @click="add" v-if="user.role === 0 || user.role === 1">新增</el-button>
            </div>
        </div>
        <el-table
                :data="tableData"
                border
                stripe
                style="width: 100%">
            <el-table-column
                    prop="houseAddress"
                    label="房屋地址"/>
            <el-table-column
                    prop="type"
                    label="房屋类型"/>
            <el-table-column
                    prop="suitableNumber"
                    label="可住人数"/>
            <el-table-column
                    prop="rent"
                    label="月租金"/>
            <el-table-column
                    prop="photo"
                    label="房屋照片">
                <template #default="scope">
                    <el-image
                            style="width: 100px"
                            :src="scope.row.photo"
                            :preview-src-list="[scope.row.photo]">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                    v-if="user.role === 2"
                    label="操作">
                <template #default="scope">
                    <el-popconfirm title="确认取消租房申请吗？" @confirm="cancelWantRent(scope.row.id)">
                        <template #reference>
                            <el-button size="mini" type="danger">取消申请</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <!--  分页  -->
        <div style="margin: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
    </div>

</template>

<style>
    .flex-bar{
        display: flex;
        flex-direction: row;
        align-items: center;
    }
</style>

<script>

    import request from "../utils/request";

    export default {
        name: 'WantRent',
        components: {},
        data() {
            return {
                form: {},
                dialogVisible: false,
                search: '',
                currentPage: 1,
                pageSize: 10,
                total: 0,
                tableData: [],
                user: {},
                title: '',
            }
        },
        created() {
            let ownerStr = sessionStorage.getItem("user") || "{}";
            this.user = JSON.parse(ownerStr);

            //请求服务端，确认当前登录用户的合法信息
            request.get("/user/" + this.user.id).then(res => {
                if (res.code === '0') {
                    this.user = res.data;
                }
            });
            this.load();
        },
        methods: {
            cancelWantRent(id) {
                request.delete("/wantRent/cancel/" + id + "/" + this.user.id).then(res => {
                    if (res.code == '0') {
                        this.$message({
                            type: "success",
                            message: "取消意向租房成功"
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: res.msg
                        })
                    }
                });
                request.delete("/meeting/" + id).then(res => {
                    if (res.code == '0') {
                        this.$message({
                            type: "success",
                            message: "取消会面成功"
                        });
                        this.load();   //刷新表格的数据
                    } else {
                        this.$message({
                            type: "error",
                            message: res.msg
                        })
                    }
                });
            },
            filesUploadSuccess(res) {
                console.log(res);
                this.form.photo = res.data;
            },
            load() {
                //登陆的是租客，则能看到自己选择的意向房屋
                request.get("/house/findWantRent", {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        id: this.user.id,
                        search: this.search
                    }
                }).then(res => {
                    console.log(res)
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                })
            },
            handleSizeChange(pageSize) {    //改变当前每页个数触发
                this.pageSize = pageSize;
                this.load();
            },
            handleCurrentChange(pageNum) {  //改变当前页码触发
                this.currentPage = pageNum;
                this.load();
            }
        }
    }
</script>